/****************************************************************

kernel/include/kernel.h
Core kernel declarations for AWOS

Copyright (c) 2006 Andrew Wilcox. All rights reserved.

Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

3. The end-user documentation included with the redistribution,
if any, must include the following acknowledgment:

"This product includes software developed by Andrew Wilcox for
AWOS. (http://code.google.com/p/awos)."

Alternately, this acknowledgment may appear in the software
itself, if and wherever such third-party acknowledgments normally
appear.

4. The names "AWOS" and "Andrew Wilcox" must not be used to
endorse or promote products derived from this software without
prior written permission. For written permission, please contact
Andrew.R.Wilcox@Gmail.com.

5. Products derived from this software may not be called "AWOS"
 nor may "AWOS" appear in their name, without prior written
 permission of Andrew Wilcox.

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ANDREW WILCOX BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.

****************************************************************/

#ifndef ____KERNEL_H_
#define ____KERNEL_H_

#include <awtypes.h>

struct regs
{
	unsigned int ds, es, fs, gs;
	unsigned int edi, esi, ebp, esp, ebx, edx, ecx, eax;
	unsigned int int_no, err_code;
	char *eip;
	unsigned int cs, eflags, useresp, ss;
};

typedef struct multiboot_info
{
	unsigned long flags;
	unsigned long mem_lower;
	unsigned long mem_upper;
	unsigned long boot_device;
	unsigned long cmdline;
	unsigned long mods_count;
	unsigned long mods_addr;
} multiboot_info_t;

struct KiMemStats
{
	ssize_t NumAlloc;	//number of times malloc() has been called
	ssize_t NumFree;	//number of times free() has been called
	ssize_t CurAlloc;	//curent allocated memory
	ssize_t TotalFree;	//total free memory
	ssize_t MaxFreeBlk;	//maximum amount of free memory in one block
	ssize_t TotalMemory;	//total memory available
};

/* These defines go to KiCpuInfo.features.c_flags */
/** supports SSE3 **/
#define CPUCAP_C_SSE3		0x00000001
/** support MONITOR/MWAIT **/
#define CPUCAP_C_MONITOR	0x00000008
/** qualified debug store (???) **/
#define CPUCAP_C_DSCPL		0x00000010
/** enhanced SpeedStep technology **/
#define CPUCAP_C_EST		0x00000080
/** thermal monitor 2 (???) **/
#define CPUCAP_C_TM2		0x00000010
/** L1 cache context can be set **/
#define CPUCAP_C_CNXTID		0x00000040

/* These defines go to KiCpuInfo.features.d_flags */
/** FPU on chip **/
#define CPUCAP_D_FPU		0x00000001
/** V86 mode enhancement support **/
#define CPUCAP_D_VME		0x00000002
/** debugging extension support **/
#define CPUCAP_D_DE		0x00000004
/** page size extension (e.g. 4MiB pages supported) **/
#define CPUCAP_D_PSE		0x00000008
/** time stamp counter support **/
#define CPUCAP_D_TSC		0x00000010
/** MSR support **/
#define CPUCAP_D_MSR		0x00000020
/** >32-bit address support **/
#define CPUCAP_D_PAE		0x00000040
/** machine check support **/
#define CPUCAP_D_MCE		0x00000080
/** cmpxchg8b support **/
#define CPUCAP_D_CX8		0x00000100
/** APIC on chip **/
#define CPUCAP_D_APIC		0x00000200
//bit 10 is reserved
/** SYS[ENTER/EXIT] supported **/
#define CPUCAP_D_SEP		0x00000800
/** MTRR registers are supported **/
#define CPUCAP_D_MTRR		0x00001000
/** global page table support **/
#define CPUCAP_D_PGE		0x00002000
/** machine check common error reporting support **/
#define CPUCAP_D_MCA		0x00004000
/** conditional move support **/
#define CPUCAP_D_CMOV		0x00008000
/** page attribute table supported **/
#define CPUCAP_D_PAT		0x00010000
/** 36-bit page sizes **/
#define CPUCAP_D_PSE36		0x00020000
/** processor serial number **/
#define CPUCAP_D_PSN		0x00040000
/** CLFLUSH supported **/
#define CPUCAP_D_CLFSH		0x00080000
//bit 20 is reserved
/** debug store support (???) **/
#define CPUCAP_D_DS		0x00200000
/** thermal MSRs **/
#define CPUCAP_D_ACPI		0x00400000
/** MMX technology support **/
#define CPUCAP_D_MMX		0x00800000
/** floating point context saves (FX[SAVE/RSTOR]) **/
#define CPUCAP_D_FXSR		0x01000000
/** SSE support **/
#define CPUCAP_D_SSE		0x02000000
/** SSE2 support **/
#define CPUCAP_D_SSE2		0x04000000
/** self snoop (???) **/
#define CPUCAP_D_SS		0x08000000
/** HyperThreading technology **/
#define CPUCAP_D_HTT		0x10000000
/** thermal monitor **/
#define CPUCAP_D_TM		0x20000000
//bit 30 is reserved
/** pending break enable (???) **/
#define CPUCAP_D_PBE		0x80000000

struct KiCpuInfo
{
	char mfr[13];
	struct
	{
		char stepping;
		char model;
		char family;
		char type;
		char ext_model;
		char ext_family;
		char brand;
		long c_flags;
		long d_flags;
	} features;
};

void KiAddToMemoryPool(void *, size_t);
void KiGetMemStats(struct KiMemStats *);
void KiDumpHeap();

void KiIrqInstall(int irq, void(*handler)(struct regs *r));
void KiIrqUninstall(int irq);

void *kmalloc(ssize_t);
void kfree(void *);
int KiGetNumAlloc(BOOL);

void printf(const char *, ...)__attribute__((format(printf,1,2)));
void dprintf(const char *, ...)__attribute__((format(printf,1,2)));
void lprintf(const char *, ...)__attribute__((format(printf,1,2)));

void KiVgaSetTextColor(unsigned char, unsigned char);
void KiVgaUpdateCursor();
void clrscr();
void putch(unsigned char);

#endif
